﻿using UnityEngine;
using System.Collections;

public class OnClickTile : OnClick
{
    PlayerManager Player;
    Node ThisGONode;
    Renderer ThisGORenderer;

    void Start()
    {
        Player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerManager>();
        ThisGONode = gameObject.GetComponent<Node>();
        ThisGORenderer = gameObject.GetComponentInChildren<Renderer>();
    }
	protected override void WindowPopup(int windowID)
    {
		base.WindowPopup(windowID);
        if (Touched)
        {
            GUI.DrawTexture(new Rect(0, 0, 100, 100), Move);
            if (GUI.Button(new Rect(0, 0, 100, 100), "Move: " + Player.Path.Length, style))
            {
                Player.UnlockAtualNode();
                Player.Walking = true;
                ThisGORenderer.material.color = Color.white;
                Touched = false;
            }
            GUI.DrawTexture(new Rect(50, 100, 100, 100), Cancel);
            if (GUI.Button(new Rect(50, 100, 100, 100), "Cancel", style))
            {
                Touched = false;
                ThisGORenderer.material.color = Color.white;
            }
        }
        else
        {
            Touched = false;
            ThisGORenderer.material.color = Color.white;
        }
    }
    protected override void OnGUI()
    {
        base.OnGUI();
        if (Touched)
        {
			windowRect = GUI.Window(0, new Rect(screenPos.x - 75, screenPos.y - 100, 150, 200), WindowPopup, "", style);
        }
    }
    protected override void OnMouseDown()
    {
        if (!Player.Walking && Vector3.Distance(gameObject.transform.position, Player.gameObject.transform.position) <= Player.stats.ViewDistance)//&& !OutOfRange
        {
            Player.GoToPos(ThisGONode);
            base.OnMouseDown();
            ThisGORenderer.material.color = Color.green;
        }
    }
    protected override void OnMouseEnter()
    {
        base.OnMouseEnter();
        if (!Touched&&Vector3.Distance(gameObject.transform.position,Player.gameObject.transform.position) <= Player.stats.ViewDistance)
        {
            ThisGORenderer.material.color = Color.yellow;
        }
    }
    protected override void OnMouseExit()
    {
        base.OnMouseExit();
        if (!Touched && Vector3.Distance(gameObject.transform.position, Player.gameObject.transform.position) <= Player.stats.ViewDistance)
        {
            ThisGORenderer.material.color = Color.white;
        }
    }
}
